package com.atguigu.web; /**
 * @Description
 * @author Chen
 * @Version
 * @create 2021-10-20-18:02
 */

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet(name = "userServlet", value = "/userServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * 退出登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void loginOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 销毁登录信息
        req.getSession().invalidate();
        // 重定向到首页
        resp.sendRedirect("");
    }

    /**
     * 处理登录功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String code = req.getParameter("code");
        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        user = userService.login(user);

        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if(token != null && token.equalsIgnoreCase(code)){
            if (user != null) {
                // 登录成功
                req.getSession().setAttribute("user", user);
                resp.sendRedirect("");
            } else {
                req.setAttribute("msg", "用户名或者密码错误！！");
                req.setAttribute("username", username);
                RequestDispatcher requestDispatcher = req.getRequestDispatcher("/view/user/login.jsp");
                requestDispatcher.forward(req, resp);
            }
        }else{
            req.setAttribute("msg", "验证码错误！！");
            req.setAttribute("username", username);
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/view/user/login.jsp");
            requestDispatcher.forward(req, resp);
        }

    }

    /**
     * 处理注册功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String code = req.getParameter("code");

        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (token != null && token.equalsIgnoreCase(code)) {
            if (!userService.existsUserName(username)) {
                userService.registerUser(user);
                req.setAttribute("msg", "注册成功,请登录");
                RequestDispatcher requestDispatcher = req.getRequestDispatcher("/view/user/login.jsp");
                requestDispatcher.forward(req, resp);
            } else {
                req.setAttribute("msg", "用户名已重复！！");
                RequestDispatcher requestDispatcher = req.getRequestDispatcher("/view/user/register.jsp");
                requestDispatcher.forward(req, resp);
            }
        } else {
            req.setAttribute("msg", "验证码错误！！");
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/view/user/register.jsp");
            requestDispatcher.forward(req, resp);
        }
    }


    /**
     * 验证用户名是否重复
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxExitsUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取请求参数
        String username = req.getParameter("username");

        boolean exits = userService.existsUserName(username);

        Map<String, Object> map = new HashMap<>();

        map.put("exits", exits);

        Gson gson = new Gson();

        String s = gson.toJson(map);

        resp.getWriter().write(s);
    }
}
